package org.dtrd.modules.food.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.dtrd.modules.food.entity.bean.CookbooksDetail;
import org.dtrd.modules.food.entity.bean.CookbooksListInfo;
import org.dtrd.modules.food.entity.po.DtrdExtEmsYwCookbooks;
import org.dtrd.modules.food.entity.request.CookbooksPageRequest;
import org.dtrd.modules.food.mapper.DtrdExtEmsYwCookbooksMapper;
import org.dtrd.modules.food.service.IDtrdExtEmsYwCookbooksService;
import org.dtrd.common.util.StringUtil;
import org.springframework.stereotype.Service;

import java.util.Optional;


/**
 * <p>
 * 菜谱 服务实现类
 * </p>
 *
 * @author QR
 * @since 2021-12-09
 */
@DS("multi-datasource1")
@Service
public class DtrdExtEmsYwCookbooksServiceImpl extends ServiceImpl<DtrdExtEmsYwCookbooksMapper, DtrdExtEmsYwCookbooks> implements IDtrdExtEmsYwCookbooksService {

    private DtrdExtEmsYwCookbooksMapper dtrdExtEmsYwCookbooksMapper;

    @Override
    public IPage<CookbooksListInfo> pageCookbooks(CookbooksPageRequest request) {
        LambdaQueryWrapper<DtrdExtEmsYwCookbooks> queryWrapper = baseQueryWrapper();
        String cookBookName = request.getCookBookName();
        if (StrUtil.isNotBlank(cookBookName)) {
            queryWrapper.like(DtrdExtEmsYwCookbooks::getCookName, StringUtil.fuzzySearchWrapper(cookBookName));
        }
        Integer cookCategoryId = request.getCookCategoryId();
        if (cookCategoryId != null) {
            queryWrapper.eq(DtrdExtEmsYwCookbooks::getCookCategoryid, cookCategoryId);
        }
        IPage<DtrdExtEmsYwCookbooks> page = new Page<>(request.getPageNum(), request.getPageSize());
        page = this.page(page, queryWrapper);
        return page.convert(CookbooksListInfo::new);
    }

    @Override
    public CookbooksDetail getCookbooks(Integer dataId) {
        DtrdExtEmsYwCookbooks dtrdExtEmsYwCookbooks = getById(dataId);
        return Optional.ofNullable(dtrdExtEmsYwCookbooks)
                        .map(po -> new CookbooksDetail().parseFromPo(po))
                        .orElse(null);
    }

    @Override
    public boolean saveOrUpdateCookbooks(CookbooksDetail dto) {
        DtrdExtEmsYwCookbooks dtrdExtEmsYwCookbooks = new DtrdExtEmsYwCookbooks();
        if(dto.getDataId() == null){
            dtrdExtEmsYwCookbooks = dtrdExtEmsYwCookbooks.init();
        }
        dtrdExtEmsYwCookbooks.parseFromDto(dto);
        return saveOrUpdate(dtrdExtEmsYwCookbooks);
    }

    @Override
    public boolean removeCookbooks(Integer dataId) {
        return removeById(dataId);
    }

    private LambdaQueryWrapper<DtrdExtEmsYwCookbooks> baseQueryWrapper() {
        return Wrappers.lambdaQuery(DtrdExtEmsYwCookbooks.class)
                        .eq(DtrdExtEmsYwCookbooks::getIsDel, 0);
    }

    private LambdaUpdateWrapper<DtrdExtEmsYwCookbooks> baseUpdateWrapper() {
        return Wrappers.lambdaUpdate(DtrdExtEmsYwCookbooks.class)
                        .eq(DtrdExtEmsYwCookbooks::getIsDel, 0);
    }
}
